<?php
/**
 * 蜻蜓数据导出CSV
 * @author yaoli<yaoli@pateo.com.cn>
 * @version 1.0 GetUser add by yaoli<yaoli@pateo.com.cn> 
 */
class radio extends ActionBase {
	
	private $date = null;
	
	function init() {
	
	}
	
	/**
	 * 动作
	 *
	 * @see AActionBase::action()
	 */
	public function action() {
		#error_reporting(E_ALL);
		
	}
	
	
	
	
	public function index(){
		
		//获取电台基础信息CSV内容
		$radioData =getCSVdata('C:\Users\dell\Desktop\radio2013-8-26\Radio_20130825.csv');
		$radiocount = $count = 0;
		foreach($radioData as $key=>$value){
			$radioid = '';				
			
			$count++;
			$radioid = $this->makeRadiosql($value['name'], $value['channel'], $value['description'], $value['check_state'],$value['radioid']);
			
			if($radioid){
				$radiocount++;
				$this->makeFreqsql($radioid, $value['frequency'],$value['city'],$value['province'],$value['cover_class']);
			}
		}
		dumpe("数据量:".$count."插入数据蛳：".$radiocount);
		
	}
	
	public function program(){
		//获取节目单CSV内容
		$programData =getCSVdata('C:\Users\dell\Desktop\radio2013-8-26\Program_20130826-14_19_11.csv');
		$updatestate = $playbillcount = $radiocount = $count = 0;
		foreach($programData as $key=>$value){
			
			$count++;
			echo $count;echo "<br>";
			if($value['radioid']>0){
				
				
				$radio = load_model("radio")->getTable(array("qtid"=>$value['radioid']),1);
			
				if(isset($radio[0])){
					$radioid = $radio[0]['id'];
					
					
					if(strlen($radio[0]['play_url'])==0){
						
						$updatestate = load_model("radio")->updateTable(array("play_url"=>$value['playurl']),array("id"=>$radioid));
						
					}
					
					if(($updatestate)>0){
						$radiocount++;
					}
						$end_time='';
						
						if(($programData[$key+1]['radioid']==$value['radioid'])&&$programData[$key+1]['broadcasttime']){
							$end_time=$programData[$key+1]['broadcasttime'];
						}
						
						$programId = $this->makeProgramsql($radioid, $value['title'],$value['broadcasttime'], $end_time, $value['broadcaster']);
						if($programId){						
							$playbillcount++;
						}
				}
			}
		}
		
		dumpe("数据量:".$count."更新电台数据：".$radiocount."新加电台：".$playbillcount);
		
	}
	
	function makeProgramsql($radioid,$title,$start_time,$end_time,$broadcaster){
		$program['data'] = array ("radio_id" => $radioid,
				"title" => addslashes($title),
				"start_time" => $start_time,
				"end_time" => $end_time,
				"broadcaster" => $broadcaster,
				"create_time" => time (),
				"update_time" => time () );
		$programId = load_model("playbill")->replaceTable($program);
		return $programId;
	}
	
	
	function makeRadiosql($name,$channel,$description,$check_state,$play_url){
		$radiosarr['data'] = array ("name" => $name, "channel" => $channel, "description" => addslashes ( $description ), "qtid" =>$play_url , 'check_state' => $check_state, "source" => 2, "create_time" => time (), "update_time" => time () );
		
		$radioid = load_model("radio")->replaceTable($radiosarr);
		
		return $radioid;
		
		
	}
	
	function makeFreqsql($radioid,$frequency,$city,$province,$cover_class){
		
		$isfmam = '';
		
		$radioFreqsarr = $this->isFMAM($frequency);
		
		$frequency = $radioFreqsarr['frequency'];
		$isfmam = $radioFreqsarr['freq_type'];
		
		
		if(abslength($city)>0){
			
			if(in_array($province, array('香港','澳门'))){
			
			}else{
				$city.="市";
			}
			
		}
		
		if(abslength($province)>0){
			if(in_array($province, array('北京','上海','天津','重庆','香港','澳门'))){
				
			}else{
				$province.="省";
			}
		}
		
		if(abslength($cover_class)>0){
			$cover_class="省";
		}
		
		// 入tbl_radio_freq表
		$radioFreqsarr['data'] = array ("radio_id" => $radioid, "freq_type" => $isfmam, "frequency" => $frequency, "country" => 'ZH_CN', "province" => $province,"city"=>$city, "cover_class" =>$cover_class, "create_time" => time (), "update_time" => time () );

		$freq_id = load_model("radio_freq")->replaceTable($radioFreqsarr);
		
		return $freq_id;
		
		
	}
	
	
	
	
	public function isFMAM($frequency){
		$radioFreqsarr = array();
		
		$radioFreqsarr['freq_type']='';
		$radioFreqsarr['frequency']='';
		
			$posFM = strpos($frequency, "FM");	
				
			if(($posFM)===0){
				$frequency = trim(str_replace("FM", '', $frequency));	
			}
			
			$posAM = strpos($frequency, "AM");				
			if(($posAM)===0){
				$frequency = trim(str_replace("AM", '', $frequency));
			}
			
			#dump($radioFreqsarr);
			
	
		
		if(is_numeric($frequency)){
			if(522<$frequency&& $frequency<1710){
				$radioFreqsarr['freq_type']='AM';
				$radioFreqsarr['frequency']=$frequency;
			}elseif(87.5<$frequency&& $frequency<108){
				$radioFreqsarr['freq_type']='FM';
				$radioFreqsarr['frequency']=$frequency*1000;
			}
				
		}
		
		return $radioFreqsarr;
		
	}
	

}